1 package net.sf.bse;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.security.cert.Certificate;
32 import java.text.DateFormat;
33 import java.text.ParseException;
34 import java.util.Date;
35 import java.util.Locale;
36 import java.util.Map;
37
38 import org.bouncycastle.jce.provider.JDKX509CertificateFactory;
39 import org.bouncycastle.jce.provider.X509CertificateObject;
40
41 /***
42 * Abstract class representing a command.
43 *
44 * @author Bill Foote (bill.foote@sun.com)
45 * @version $Revision: 1.1 $ $Date: 2003/01/17 14:40:16 $
46 */
47 public abstract class Command
48 {
49 private Map arguments;
50
51 protected Command(Map arguments)
52 {
53 this.arguments = arguments;
54 }
55
56 /***
57 * @return the given argument, or null if it's not there
58 */
59 protected String getArg(String key)
60 {
61 return (String)arguments.get(key);
62 }
63
64 protected Date getDateArg(String key)
65 {
66 DateFormat df =
67 DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRANCE);
68 try
69 {
70 df.setLenient(false);
71 return df.parse(getArg(key));
72 }
73 catch (ParseException ex)
74 {
75 System.err.println();
76 System.err.println(
77 "Cannot parse the date given for \"" + key + "\".");
78 System.err.println(ex.toString());
79 System.err.println();
80 System.exit(1);
81 return null;
82 }
83 }
84
85 protected byte[] readBytesFromFile(String fn) throws IOException
86 {
87 FileInputStream fis = new FileInputStream(fn);
88 ByteArrayOutputStream bos = new ByteArrayOutputStream();
89 int ch;
90 while ((ch = fis.read()) != -1)
91 {
92 bos.write(ch);
93 }
94 return bos.toByteArray();
95 }
96
97 protected X509CertificateObject readX509(ByteArrayInputStream bis)
98 throws Exception
99 {
100 JDKX509CertificateFactory cf = new JDKX509CertificateFactory();
101
102
103
104
105 Certificate cert = cf.engineGenerateCertificate(bis);
106 return (X509CertificateObject) cert;
107 }
108
109 public abstract void usageMessage(PrintStream out);
110
111 public abstract String[] getRequiredArgs();
112
113 public abstract String[] getOptionalArgs();
114
115 /***
116 * Execute this command.
117 */
118 public abstract void run() throws Exception;
119 }